package com.un.ebs.businessObject.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.un.ebs.core.enums.DataTypeEnum;
import com.un.ebs.core.enums.DbTypeEnum;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.domain.Column;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zc
 * @ClassName AbstractDbService
 * @Description dbservice 抽象类
 * @Date 2021/1/13
 */
public class SQLSimpleHandler {

    protected String getColumnSQL(List<Column> list) {
        StringBuffer selectSQL = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            Column column = list.get(i);
            String columnName = column.getColumnName();
            if (i == list.size() - 1) {
                selectSQL.append(columnName);
            } else {
                selectSQL.append(columnName + ",");
            }
        }
        return selectSQL.toString();
    }

    protected String getConditionSQL(String sortName, Map<String, Object> params, List<Column> columnList) {
        StringBuffer conditionSQL = new StringBuffer();
        String orderByStr = "";
       
        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            if(!CollectionUtil.isEmpty(params)){
                Object o = params.get(column.getColumnName());
                if (o != null) {
                    int code = column.getColumnType().getCode();
                    //字符类型 默认使用like拼接
                    if (DataTypeEnum.STRING.getCode() == code) {
                        conditionSQL.append(" AND " + column.getColumnName() + " = ");
                        conditionSQL.append(o);
                    } else if (DataTypeEnum.DATE.getCode() == code) {
                        //日期类型 使用区间处理
                        conditionSQL.append(" AND " + column.getColumnName() + " like ");
                        conditionSQL.append("%");
                        conditionSQL.append(o);
                        conditionSQL.append("%");
                    } else {
                        //其他类型 使用 "=" 处理
                        conditionSQL.append(" AND " + column.getColumnName() + " = ");
                        conditionSQL.append(o);
                    }
                }
            }
            if(!StringUtils.isNullOrEmpty(sortName)) {
                if (column.getColumnName().equals(sortName)) {
                    orderByStr = " order by " + column.getColumnName() + " desc ";
                }
            }
        }
        return conditionSQL.append(orderByStr).toString();
    }

    protected String getPageSQL(DbTypeEnum dbType, Integer pageSize, Integer pageIndex) {
        StringBuffer pageSQL = new StringBuffer();
        Integer num = (pageIndex - 1) * pageSize;
        Integer size = pageSize;
        if (DbTypeEnum.mysql.equals(dbType)) {
            pageSQL.append(String.format(" limit %d,%d ", num, size));
        }
        return pageSQL.toString();
    }

    protected Map<String, String> getInsertColumn(JSONObject json, List<Column> columnList) {
        Map<String, String> resultMap = new HashMap<>();
        StringBuffer keys = new StringBuffer();
        StringBuffer values = new StringBuffer();
        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String key = column.getColumnName();
            String value = json.getString(column.getColumnName());
            if (!StringUtils.isNullOrEmpty(value)) {
                if (i != columnList.size() - 1) {
                    values.append(value + ",");
                    keys.append(key + ",");
                } else {
                    values.append(value);
                    keys.append(key);
                }
            }
        }
        resultMap.put("keys", keys.toString());
        resultMap.put("values", values.toString());
        return resultMap;
    }

    protected String getUpdateColumn(JSONObject json, List<Column> columnList) {
        StringBuffer sql = new StringBuffer();
        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String key = column.getColumnName();
            String value = json.getString(column.getColumnName());
            if (!StringUtils.isNullOrEmpty(value)) {
                if (i == 0 || StringUtils.isNullOrEmpty(sql.toString())) {
                    sql.append(" set " + key + "=" + value);
                } else if (i > 0) {
                    sql.append("," + key + "=" + value);
                }
            }
        }
        return sql.toString();
    }

}
